package com.yeyaomai.dksns.flowis.quartz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.yeyaomai.dksns.flowis.dao.DkUserInfoDao;
import com.yeyaomai.dksns.flowis.dao.LoginProfileDao;
import com.yeyaomai.dksns.flowis.domain.DkUserInfo;

public class DkUserInfoExecutor {

	@Resource
	private LoginProfileDao loginProfileDao;
	@Resource
	private DkUserInfoDao dkUserInfoDao;

	final Logger logger = Logger.getLogger("quartz");
	/**
	 * 按天统计端口用户数据
	 * 
	 * @throws ParseException
	 */
	public void saveUserInfoByDay() throws ParseException {
		Date date = new Date();
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
		Integer endTime = (int) (ff.parse(ff.format(date)).getTime() / 1000);
		Integer startTime = endTime - 86400;
		List<DkUserInfo> userList = loginProfileDao.getDkUserInfo(startTime,endTime);
		logger.info("###########按天统计端口用户数据，写入数据################::saveUserInfoByDay()"+new Date());
		dkUserInfoDao.addUserByDay(userList);
		logger.info("###########按天统计端口用户数据，完成数据################::saveTrendAnalysisByDay()"+new Date());
	}

	/**
	 * 按周统计端口用户数据 每天统计一次，向前推七天统计
	 * 
	 * @throws ParseException
	 */
	public void saveUserInfoByWeek() throws ParseException {
		Date date = new Date();
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
		Integer endTime = (int) (ff.parse(ff.format(date)).getTime() / 1000);
		Integer startTime = endTime - 604800;
		List<DkUserInfo> userList = loginProfileDao.getDkUserInfo(startTime,endTime);
		logger.info("###########按周统计端口用户数据，写入数据################::saveUserInfoByWeek()"+new Date());
		dkUserInfoDao.addUserByWeek(userList);
		logger.info("###########按周统计端口用户数据，完成数据################::saveUserInfoByWeek()"+new Date());
	}

	/**
	 * 按月统计数据
	 * 
	 * @throws ParseException
	 */
	public void saveUserInfoByMonth() throws ParseException {
		Date date = new Date();
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
		Integer endTime = (int) (ff.parse(ff.format(date)).getTime() / 1000);
		Long time = Long.valueOf(endTime - 86400);
		String end = ff.format(new Date(time * 1000));
		String start = end.replace(end.substring(end.lastIndexOf("-") + 1),
				"01");
		Integer startTime = (int) (ff.parse(start).getTime() / 1000);
		logger.info("###########按月统计端口用户数据，写入数据################::saveUserInfoByMonth()"+new Date());
		List<DkUserInfo> userList = loginProfileDao.getDkUserInfo(startTime,endTime);
		dkUserInfoDao.addUserByMonth(userList);
		logger.info("###########按月统计端口用户数据，完成数据################::saveUserInfoByMonth()"+new Date());
	}
}
